// Code file original author: Martin Lapierre, http://devinstinct.com

using System;
using System.Collections.Generic;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation.Properties;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using EntLibContrib.Validation.Validators;

namespace EntLibContrib.Validation.Configuration
{
    public class CompositeRulesetValidatorData : ValidatorData
    {
        private const string TargetRulesetsPropertyName = "targetRulesets";
        private const string SourcePropertyName = "source";
        private const string ConfigurationSourceNamePropertyName = "sourceConfigurationName";

        public CompositeRulesetValidatorData()
        { }

        public CompositeRulesetValidatorData(string name)
            : base(name, typeof(CompositeRulesetValidator))
        { }

        [ConfigurationProperty(TargetRulesetsPropertyName)]
        public string TargetRulesets
        {
            get { return (string)this[TargetRulesetsPropertyName]; }
            set { this[TargetRulesetsPropertyName] = value; }
        }

        [ConfigurationProperty(SourcePropertyName, DefaultValue=ValidationSpecificationSource.Both)]
        public ValidationSpecificationSource Source
        {
            get { return (ValidationSpecificationSource)this[SourcePropertyName]; }
            set { this[SourcePropertyName] = value; }
        }

        [ConfigurationProperty(ConfigurationSourceNamePropertyName)]
        public string ConfigurationSourceName
        {
            get { return (string)this[ConfigurationSourceNamePropertyName]; }
            set { this[ConfigurationSourceNamePropertyName] = value; }
        }

        /// <summary>
        /// Creates the <see cref="CollectionCountValidator"/> described by the configuration object.
        /// </summary>
        /// <param name="targetType">The type of object that will be validated by the validator.</param>
        /// <returns>The created <see cref="CollectionCountValidator"/>.</returns>
        protected override Validator DoCreateValidator(Type targetType)
        {
            return new CompositeRulesetValidator(
                targetType, 
                CompositeRulesetValidator.GetTargetRulesets(TargetRulesets),
                Source,
                ConfigurationSourceName);
        }
    }
}